package com.example.Windturbine.Controller;

import com.alibaba.excel.EasyExcel;
import com.example.Windturbine.Controller.ViewObject.Excel.ManufactureExcel;
import com.example.Windturbine.Controller.ViewObject.Excel.MaterialExcel;
import com.example.Windturbine.Controller.ViewObject.Excel.QAExcel;
import com.example.Windturbine.Controller.ViewObject.MaterialInfoVO;
import com.example.Windturbine.Controller.ViewObject.MaterialVO;
import com.example.Windturbine.Dao.QADOMapper;
import com.example.Windturbine.DataObject.ManufactureDO;
import com.example.Windturbine.DataObject.ManufactureInfoDO;
import com.example.Windturbine.Error.BusinessException;
import com.example.Windturbine.Error.EmBusinessError;
import com.example.Windturbine.Listener.ExceManufactureListener;
import com.example.Windturbine.Listener.ExcelMaterialListener;
import com.example.Windturbine.Listener.ExcelQAListener;
import com.example.Windturbine.Response.CommonResult;
import com.example.Windturbine.Service.Impl.ManufactureServiceImpl;
import com.example.Windturbine.Service.ManufactureService;
import com.example.Windturbine.Service.Model.ManufactureModel;
import com.example.Windturbine.Service.Model.MaterialModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Set;

import static com.example.Windturbine.Controller.BaseController.CONTENT_TYPE_FORMED;

/**
 * @Author 丁永新
 * @Date 2021/12/20
 */
@Slf4j
@Controller("manufacture")
@RequestMapping("/manufacture")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class ManufactureController {

    @Autowired
    ManufactureService manufactureService;

    @Resource
    QADOMapper qadoMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("/get")
    @ResponseBody
    public CommonResult getAllManufacture(@RequestParam(name = "id") Integer id) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
        List<ManufactureDO> res = manufactureService.getAll(id);
        return CommonResult.create(res);
    }

    @RequestMapping(value = "/submit", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
    @ResponseBody
    public CommonResult submit(@RequestParam(name = "man_name") String man_name,
                               @RequestParam(name = "man_type") String man_type,
                               @RequestParam(name = "man_tool") String man_tool,
                               @RequestParam(name = "man_power") Integer man_power,
                               @RequestParam(name = "man_material") String man_material,
                               @RequestParam(name = "man_unit") String man_unit,
                               @RequestParam(name = "man_percent") Double man_percent,
                               @RequestParam(name = "man_origin") String man_origin,
                               @RequestParam(name = "man_comments") String man_comments,


                               @RequestParam(name = "coal") BigDecimal coal,
                               @RequestParam(name = "oil") BigDecimal oil,
                               @RequestParam(name = "gas") BigDecimal gas,
                               @RequestParam(name = "co") BigDecimal co,
                               @RequestParam(name = "co2") BigDecimal co2,
                               @RequestParam(name = "so2") BigDecimal so2,
                               @RequestParam(name = "nox") BigDecimal nox,
                               @RequestParam(name = "ch4") BigDecimal ch4,
                               @RequestParam(name = "h2s") BigDecimal h2s,
                               @RequestParam(name = "cod") BigDecimal cod,
                               @RequestParam(name = "hcl") BigDecimal hcl,
                               @RequestParam(name = "nh3") BigDecimal nh3,
                               @RequestParam(name = "cfc11") BigDecimal cfc11,
                               @RequestParam(name = "cfc12") BigDecimal cfc12,
                               @RequestParam(name = "cfc113") BigDecimal cfc113,
                               @RequestParam(name = "user_id") Integer id) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {

        ManufactureModel manufactureModel = new ManufactureModel();
        manufactureModel.setName(man_name);
        manufactureModel.setType(man_type);
        manufactureModel.setMachineTool(man_tool);
        manufactureModel.setPower(man_power);
        manufactureModel.setMaterial(man_material);
        manufactureModel.setUnit(man_unit);
        manufactureModel.setPercent(man_percent);
        manufactureModel.setOrigin(man_origin);
        manufactureModel.setComments(man_comments);
        manufactureModel.set煤(coal);
        manufactureModel.set天然气(gas);
        manufactureModel.set石油(oil);
        manufactureModel.setCo(co);
        manufactureModel.setCo2(co2);
        manufactureModel.setSo2(so2);
        manufactureModel.setNox(nox);
        manufactureModel.setCh4(ch4);
        manufactureModel.setH2s(h2s);
        manufactureModel.setCod(cod);
        manufactureModel.setHcl(hcl);
        manufactureModel.setNh3(nh3);
        manufactureModel.setCfc11(cfc11);
        manufactureModel.setCfc12(cfc12);
        manufactureModel.setCfc113(cfc113);
        manufactureModel.setLevel(id);


        manufactureService.submitSingle(manufactureModel);

        return CommonResult.create(null);
    }

    @RequestMapping("/getdetail")
    @ResponseBody
    public CommonResult getDeatailById(@RequestParam(name = "id") Integer id) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
        ManufactureModel manufactureInfoDO = manufactureService.getDetailById(id);
        return CommonResult.create(manufactureInfoDO);
    }

    @RequestMapping("/deletebyid")
    @ResponseBody
    public CommonResult deleteById(@RequestParam(name = "id") Integer id) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
        manufactureService.deleteById(id);
        return CommonResult.create(null);
    }

    @RequestMapping(value = "/submitfile", method = {RequestMethod.POST}, consumes = {"application/x-www-form-urlencoded", "multipart/form-data"})
    @ResponseBody
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
    public CommonResult submitProduct(@RequestParam(name = "userVOIdFile") Integer userid, @RequestParam(name = "uploadfile") MultipartFile file) throws BusinessException, IOException, NoSuchAlgorithmException {
        if (file == null) {
            log.error("submitProduct：上传文件失败！");
            throw new BusinessException(new BusinessException(EmBusinessError.UNKNOWN_ERROR, "上传文件失败！"));
        }
        System.out.println(file.getOriginalFilename());
        if(file.getOriginalFilename().contains("问答")){
            InputStream in = file.getInputStream();
            try {
                //qadoMapper.deleteAll();
                EasyExcel.read(in, QAExcel.class, new ExcelQAListener(qadoMapper)).sheet().doRead();
                redisTemplate.delete("qas");
                in.close();
            } catch (Exception e) {
                throw new BusinessException(new BusinessException(EmBusinessError.UNKNOWN_ERROR, "上传问答语料文件失败！"));
            }
            return CommonResult.create("成功！");
        }else{
            InputStream in = file.getInputStream();
            try {
                EasyExcel.read(in, ManufactureExcel.class, new ExceManufactureListener(manufactureService, userid)).sheet().doRead();
                in.close();
            } catch (Exception e) {
                throw new BusinessException(new BusinessException(EmBusinessError.UNKNOWN_ERROR, "上传工艺信息文件失败！"));
            }
        }





        return CommonResult.create(null);
    }

}
